import {parseFilesFromMetaXml} from "./file";

export const getFileVersion = async (client, user, fileid)=>{
    let path = `/versions/${user.id}/versions/${fileid}`
    let files = await client.customRequest(path , {
        method: "PROPFIND",
        headers: {
            Accept: "text/xml",
            Depth: "infinity",
            "Content-Type": "application/xml; charset=utf-8"
        },
        data: `<?xml version="1.0"?>
                <d:propfind  xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
                  <d:prop>
                    <d:getcontentlength />
                    <d:getcontenttype />
                    <d:getlastmodified />
                  </d:prop>
                </d:propfind>`,
        responseType: "text"
    });
    files = parseFilesFromMetaXml(files,`/remote.php/dav/versions/${user.id}/versions/${fileid}`)
    files = files.map((file)=>{
        let {fileid,size,...obj}=file
        return obj
    })
    return files;
}

export const getFileVersionContent = async (client,user,fileid,version)=>{
    let version_path = `/versions/${user.id}/versions/${fileid}/${version}`
    let result = await client.getFileContents(version_path,{format:'text'})
    return result
}
